package com.amex.uam.action;

import java.util.HashSet;
import java.util.ResourceBundle;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.amex.uam.model.MSLogin;
import com.amex.uam.model.Message;
import com.amex.uam.service.MstrService;
import com.amex.uam.util.Encryption;
import com.amex.uam.util.UAMProp;
import com.amex.uam.util.Utils;
import com.microstrategy.web.beans.RequestKeys;
import com.microstrategy.web.objects.WebIServerSession;

@Controller("mstrAction")
public class MstrAction {
	private static boolean DEBUG = true;
	private MstrService mstrService;
	
	@Resource
	public void setMstrService(MstrService mstrService) {
		this.mstrService = mstrService;
	}
	
	public Message loadSecurityGroupByGroupName(RequestKeys keys, WebIServerSession session){
		String groupName = Utils.trim(keys.getValue("groupName"), null);
		String userId = Utils.trim(keys.getValue("userId"), null);
		log("loadSecurityGroupByGroupName", "groupName=" + groupName + ", userId=" + userId);
		return loadSecurityGroupByGroupName(userId, groupName, session);
	}
	public Message loadSecurityGroupByGroupName(String userId, String groupName, WebIServerSession session) {
		//System.out.println("MstrAction.loadSecurityGroupByGroupName->> userName: "+userName + ", groupName: " + groupName);
		try {
			if (session == null) {
				MSLogin user = new MSLogin();
				user.setServerName(UAMProp.getIServerName());
				user.setPort(Utils.parseInt(UAMProp.getIServerPort(), 0));
				user.setUsername(UAMProp.getLoginName());
				user.setPassword(Encryption.Decrypt(UAMProp.getPassword()));
				session = mstrService.getWebIServerSession(user);
			}
			if (session == null)
				return Message.newError("session is invalid");
			
			return mstrService.loadSecurityGroupByGroupName(userId, groupName, session);
		} catch (Throwable t) {
			t.printStackTrace();
			return Message.newError();
		}
	}
	
	public Message updateUserType(RequestKeys keys, WebIServerSession session) {
		String userId = Utils.trim(keys.getValue("empId"), null);
		String userTypeVal = Utils.trim(keys.getValue("usrTpRadio"), null);// modified : object
		String userTypeOri = Utils.trim(keys.getValue("ori_usrTpRadio"), null); // original : object
		//System.out.println("MstrAction.updateUserType--> userId: " + userId +", userTypeOri: "+ userTypeOri +", userTypeVal: "+ userTypeVal);
		return mstrService.updateUserGroup(userId, userTypeOri, userTypeVal, session);
	}
	
	public Message updateGMAPS(RequestKeys keys, WebIServerSession session) {
		String userId = Utils.trim(keys.getValue("empId"), null);
		String gmapsVal = Utils.trim(keys.getValue("gmapsRadio"), null); // modified : object : -1 indicates no access
		String gmapsOri = Utils.trim(keys.getValue("ori_gmapsRadio"), null);// original : object
		if ("-1".equals(gmapsVal)) {
			gmapsVal = null;
		}
		//System.out.println("MstrAction.updateGMAPS--> userId: " + userId +", userTypeOri: "+ gmapsOri +", userTypeVal: "+ gmapsVal);
		return mstrService.updateUserGroup(userId, gmapsOri, gmapsVal, session);
	}
	
	/*
	 * updateOptBlue(RequestKeys keys, WebIServerSession session)
	 * This page is only for View User Profile page
	 */
	public Message updateOptBlue(RequestKeys keys, WebIServerSession session) {
		String userId = Utils.trim(keys.getValue("empId"), null);
		System.out.println("empId::"+userId);
		String optBlueVal = Utils.trim(keys.getValue("optBlueRadio"), null);// modified : object : -1 indicates no access
		String optBlueOri = Utils.trim(keys.getValue("ori_optBlueRadio"), null); // original : object
		System.out.println("optBlueVal*************************************::"+optBlueVal);
		if ("-1".equals(optBlueVal)) {
			System.out.println("optBlueVal == null");
			optBlueVal = null;
		}/*else{
			mstrService.grantDenyACL(userId,optBlueOri, optBlueVal, session);
		}*/
		
		return mstrService.updateUserGroup(userId, optBlueOri, optBlueVal, session);
	}

	public Message updateSharedReports(RequestKeys keys, WebIServerSession session) {
		String userId = Utils.trim(keys.getValue("empId"), null);
		String sharedRpt = Utils.trim(keys.getValue("sharedRptRadio"), null);// yes:have access, no:don't have access
		String sharedRptOri = Utils.trim(keys.getValue("ori_shdRptVal"), null);// original : list
		String sharedRptNames = Utils.trim(keys.getValue("shdRptName"), null);// parameter names
		Set<String> addList = new HashSet<String>(); 
		if (sharedRptNames != null) {
			String[] paramNames = sharedRptNames.split(",");
			for (String pname : paramNames) {
				if (pname.trim().length() > 0) {
					//System.out.println("--" + pname + " : " + keys.getValue(pname));
					String pvalue = keys.getValue(pname);
					if (pvalue == null || pvalue.equals("-1") || pvalue.trim().length() == 0)
						continue;
					
					addList.add(pvalue.trim());
				}
			}
		}
		System.out.println("MstrAction.updateSharedReports -> userId: " + userId + ", sharedRptOri: " + sharedRptOri + ", addList.size: " + addList.size());
		return this.mstrService.updateSharedReports(userId, sharedRpt, sharedRptOri, addList, session);
		
	}
	
	public Message createSharedReportGroup(RequestKeys keys, WebIServerSession session) {
		String newScyGrpName = Utils.trim(keys.getValue("newScyGrpName"), null);
		return mstrService.createSharedReportGroup(newScyGrpName, session);
	}

	private void log(String methodName, String message) {
		if (DEBUG)
			System.out.println(this.getClass().getSimpleName() + "->" + methodName + ":  " + message);
	}

	public Message activateUser(RequestKeys keys, WebIServerSession session) {
		String userId = Utils.trim(keys.getValue("userIds"), null);
		String activate = Utils.trim(keys.getValue("activate"), null);
		
		return mstrService.activateUser(userId, activate, session);
	}
	
	public Message setAccessEndDate(RequestKeys keys, WebIServerSession session){
		String userId = Utils.trim(keys.getValue("empId"), null);
		String accEndDate = Utils.trim(keys.getValue("accEndDate"), null);
		return mstrService.setAccessEndDate(userId, accEndDate, session);
	}
	
}
